import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import Login from './views/Login'
import Users from './components/Users'
import Rights from './components/rights/Rights'
import Roles from './components/rights/Roles'
import Goods from './components/goods/Goods'
import Add from './components/goods/AddGoods'
import Update from './components/goods/UpdateGoods'
import Params from './components/goods/Params'
import Categories from './components/goods/Categories'
import Orders from './components/orders/Orders'
import Reports from './components/reports/Reports'

Vue.use(Router)

const router = new Router({
        routes: [{
            path: '/',
            redirect: '/login',
        }, {
            path: '/login',
            name: '/login',
            component: Login
        }, {
            path: '/home',
            name: 'home',
            component: Home,
            children: [{
                path: '/users',
                name: 'Users',
                component: Users
            }, {
                path: '/rights',
                name: 'Rights',
                component: Rights
            }, {
                path: '/roles',
                name: 'Roles',
                component: Roles
            }, {
                path: '/goods',
                name: 'Goods',
                component: Goods
            }, {
                path: '/goods/add',
                name: 'Add',
                component: Add
            }, {
                //     path: '/goods/add',
                //     name: 'Update',
                //     component: Update
                // }, {
                path: '/updategoods/:id',
                name: 'Update',
                component: Update
            }, {
                path: '/categories',
                name: 'Categories',
                component: Categories
            }, {
                path: '/params',
                name: 'Params',
                component: Params
            }, {
                path: '/orders',
                name: 'Orders',
                component: Orders
            }, {
                path: '/reports',
                name: 'Reports',
                component: Reports
            }]
        }]
    })
    //挂载路由导航守卫
router.beforeEach((to, from, next) => {
    /* 必须调用 `next` */
    if (to.path === '/login') return next()

    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()

})

export default router